package com.ccp.dev.workflow.service.bpm.impl;


import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.system.dao.PositionDao;
import com.ccp.dev.system.dao.SysOrgDao;
import com.ccp.dev.system.dao.SysUserDao;
import com.ccp.dev.system.dao.UserPositionDao;
import com.ccp.dev.system.model.Position;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.model.UserPosition;
import com.ccp.dev.workflow.model.BpmNodeUser;
import com.ccp.dev.workflow.model.bpm.CalcVars;
import com.ccp.dev.workflow.model.bpm.PreViewModel;
import com.ccp.dev.workflow.model.bpm.TaskExecutor;
import com.ccp.dev.workflow.service.bpm.IBpmNodeUserCalculation;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 根据节点用户设置为“与发起人或上一任务执行人具有相同组织的人”，计算执行人员。
 * 根据发起人或上一任务执行人的组织（包括组织、岗位、职务）计算人员。
 * <pre>
 * 人员的JSON格式如下：
 * {type:'org',userType:'start',varName:'name'}
 * type:可能的值为：
 * org 组织
 * pos 岗位
 * job 职务
 * userType:可能的值为 start、prev
 * </pre>
 * @author fanruiqi
 */
@Component
public class BpmNodeUserCalculationStartOrPrevWithOrg implements IBpmNodeUserCalculation {
	@Resource
	private SysUserDao sysUserDao;
	@Resource
	private SysOrgDao sysOrgDao;
	@Resource
	private UserPositionDao userPositionDao;
	@Resource
	private PositionDao positionDao;
//	@Resource
//	private JobDao jobDao;
//	@Resource
//	private SysUserOrgService sysUserOrgService;

	@Override
	public List<SysUser> getExecutor(BpmNodeUser bpmNodeUser, CalcVars vars) {
		String userId = null;
		String orgId  = null;
		List<SysUser> sysUsers = new ArrayList<SysUser>();
		String cmpIds 	= bpmNodeUser.getCmpIds();
		JSONObject jsonObject = JSONObject.fromObject(cmpIds);
		String type 	= jsonObject.getString("type");
		String userType = jsonObject.getString("userType");
		//发起人
		if("start".equals(userType)){
			userId=vars.getStartUserId();
		}
		//上一任务执行人
		if("prev".equals(userType)){
			userId=vars.getPrevExecUserId();
			if(userId==null||userId==""){
				userId=vars.getStartUserId();
			}
		}
		if(userId==null || userId==""){
			return new ArrayList<SysUser>();
		}
		//组织
		if("org".equals(type)){
			sysUsers = sysUserDao.queryOrgUsersByuserId(userId);
		}
		//职务
		if("job".equals(type)){
			sysUsers =sysUserDao.queryJobUsersByUserId(userId);
		}
		//岗位
		if("pos".equals(type)){
			sysUsers =sysUserDao.queryPosUsersByuserId(userId);
		}
		return sysUsers;
	}

	@Override
	public String getTitle() {
		return "与发起人或上一任务执行人具有相同组织的人";
	}
	@Override
	public Set<TaskExecutor> getTaskExecutor(BpmNodeUser bpmNodeUser, CalcVars vars) {
		String userId = null;

		String cmpIds 	= bpmNodeUser.getCmpIds();
		JSONObject jsonObject = JSONObject.fromObject(cmpIds);
		String type 	= jsonObject.getString("type");
		String userType = jsonObject.getString("userType");

		int extractUser = bpmNodeUser.getExtractUser();
		Set<TaskExecutor> uIdSet = new LinkedHashSet<>();

		//发起人
		if("start".equals(userType)){
			userId=vars.getStartUserId();
		}
		//上一任务执行人
		if("prev".equals(userType)){
			userId=vars.getPrevExecUserId();
		}
		if(userId==null || userId==""){
			return uIdSet;
		}
		//组织
		if("org".equals(type)){
			handOrg(userId,extractUser,uIdSet);
		}
		//职务
		if("job".equals(type)){
//			handJob(userId,extractUser,uIdSet);
		}
		//岗位
		if("pos".equals(type)){
			handPos(userId,extractUser,uIdSet);
		}
		return uIdSet;
	}

	@Override
	public boolean supportMockModel() {
		return false;
	}

	@Override
	public List<PreViewModel> getMockModel(BpmNodeUser bpmNodeUser) {

		List< PreViewModel> list=new ArrayList<PreViewModel>();
		PreViewModel preViewModel	 =new PreViewModel();
		preViewModel.setType(PreViewModel.START_USER);
		list.add(preViewModel);

		return null;
	}

	@Override
	public boolean supportPreView() {
		return true;
	}

	/**
	 * 得到用户的岗位
	 */
	private void handPos(String userId, int extractUser, Set<TaskExecutor> uIdSet) {
		List<Position> positionList=positionDao.queryByUserId(userId);
		if(BeanUtils.isEmpty(positionList)){ return; }

		switch (extractUser) {
			//不抽取
			case TaskExecutor.EXACT_NOEXACT:
				for(Position position:positionList){
					TaskExecutor executor=TaskExecutor.getTaskPos(position.getPosId().toString(), position.getPosName());
					uIdSet.add(executor);
				}
				break;
			//抽取
			case TaskExecutor.EXACT_EXACT_USER:
				//得到相同岗位的用户
				List<SysUser> list= sysUserDao.queryPosUsersByuserId(userId);
				for(SysUser sysUser:list){
					TaskExecutor taskExcutor=TaskExecutor.getTaskUser(sysUser.getUserId().toString(), sysUser.getFullName());
					uIdSet.add(taskExcutor);
				}
				break;
			//二次抽取
			case TaskExecutor.EXACT_EXACT_SECOND:
				for(Position position:positionList){
					TaskExecutor taskexecutor=TaskExecutor.getTaskOrg(position.getPosId().toString(), position.getPosName());
					taskexecutor.setExactType(extractUser);
					uIdSet.add(taskexecutor);
				}
				break;
			default:
				break;
		}

	}

	/**
	 * 得到用户的组织
	 */
	private void handOrg(String userId, int extractUser, Set<TaskExecutor> uIdSet) {
		UserPosition userPos = userPositionDao.getOneByUserId(userId);
		if(userPos==null){ return;}
		String orgId=	userPos.getOrgId();
		SysOrg sysOrg= sysOrgDao.getById(orgId);
		switch (extractUser) {
			//不抽取
			case TaskExecutor.EXACT_NOEXACT:
				TaskExecutor executor=TaskExecutor.getTaskOrg(orgId, sysOrg.getOrgName());
				uIdSet.add(executor);
				break;
			//抽取
			case TaskExecutor.EXACT_EXACT_USER:
				List<SysUser> list= sysUserDao.queryByOrgId(orgId);
				for(SysUser sysUser:list){
					TaskExecutor taskExcutor=TaskExecutor.getTaskUser(sysUser.getUserId(), sysUser.getFullName());
					uIdSet.add(taskExcutor);
				}
				break;
			//二次抽取
			case TaskExecutor.EXACT_EXACT_SECOND:
				TaskExecutor taskexecutor=TaskExecutor.getTaskOrg(orgId, sysOrg.getOrgName());
				taskexecutor.setExactType(extractUser);
				uIdSet.add(taskexecutor);
				break;
			default:
				break;
		}

	}






}